<template>
  <!-- 添加或修改付款记录对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="名称" prop="supplierName">
                <el-input v-model="form.supplierName" placeholder="请输入名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开户行" prop="bankName">
                <el-input v-model="form.bankName" placeholder="请输入开户行"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="行号" prop="bankNo">
                <el-input v-model="form.bankNo" placeholder="请输入行号"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="账号" prop="accountNo">
                <el-input v-model="form.accountNo" placeholder="请输入账号"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支付时间" prop="payTime">
                <el-date-picker clearable
                                v-model="form.payTime"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="请选择支付时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="付款金额" prop="money">
                <el-input-number :precision="2" v-model="form.money" :max="apply.remainMoney"
                                 placeholder="请输入付款金额"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="付款凭证" prop="attachment">
                <file-upload v-model="form.attachment" :limit="5"></file-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {accAdd, addAction, getAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';
import {parseTime} from "@/utils/common";

export default {
  name: "PayRecordShippingForm",
  dicts: ['yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      selectRows: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        buyId: null,
        invoiceCode: null,
        assessStatus: null,
        orderByColumn: "applyTime",
        isAsc: "desc"
      },
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      apply: {},
      // 表单校验
      rules: {
        supplierName: [{required: true, message: "请输入供应商名称", trigger: "blur"}],
        bankName: [{required: true, message: "请输入开户行", trigger: "blur"}],
        bankNo: [{required: true, message: "请输入开户行行号", trigger: "blur"}],
        accountNo: [{required: true, message: "请输入银行账号", trigger: "blur"}],
        payTime: [{required: true, message: "请选择支付时间", trigger: "blur"}],
        money: [
          {required: true, message: "请输入申请付款金额", trigger: "blur"},
          {
            validator: this.moneyValidator,
            trigger: 'blur'
          }
        ],
      },
      url: {
        getById: '/payRecord/payRecord/',
        add: '/payRecord/payRecord/payAddForShipping',
        edit: '/payRecord/payRecord/edit',
      },
    };
  },
  created() {

  },
  methods: {
    moneyValidator(rule, value, callback) {
      if (value === 0) {
        callback(new Error('付款金额不能为0'));
      } else {
        callback();
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyId: null,
        supplierId: null,
        supplierName: null,
        bankName: null,
        bankNo: null,
        accountNo: null,
        payTime: null,
        money: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加付款记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改付款记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.payApplyBuyDetails = this.selectRows;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("付款成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    /** 新增按钮操作 */
    handleAddByApply(apply) {
      this.reset();
      this.open = true;
      this.title = "添加付款记录";
      this.apply = apply;
      this.form.applyId = apply.id;
      this.form.supplierId = apply.supplierId;
      this.form.supplierName = apply.personName;
      this.form.bankName = apply.bankName;
      this.form.bankNo = apply.bankNo;
      this.form.accountNo = apply.accountNo;
      this.form.money = apply.remainMoney;
      this.form.payTime = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}');
      this.loading = false;
    },

  }
};
</script>
